﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RTC.Web
{
    public class RTCService
    {
        public bool Save(RTCMessage message)
        {
            if (message.Timestamp == default(DateTime))
            {
                message.Timestamp = DateTime.Now;
            }

            using (RTCDbContext context = new RTCDbContext())
            {
                context.Set<RTCMessage>().Add(message);
                if (context.SaveChanges() > 0)
                {
                    return true;
                }

                return false;
            }
        }

        public List<RTCMessage> FindUnreceivedMessage(string account)
        {
            using (RTCDbContext context = new RTCDbContext())
            {
                string sql="select m.* from RTC_Message as m join RTC_ReceiveRecord as rr on m.Timestamp > rr.Timestamp where rr.Account={0}";
                return context.Database.SqlQuery<RTCMessage>(sql, account).ToList();
            }
        }

        public bool UpdateReceiveRecord(string account, DateTime timestamp)
        {
            RTCReceiveRecord record = new RTCReceiveRecord();
            record.Account = account;
            record.Timestamp = timestamp;

            using (RTCDbContext context = new RTCDbContext())
            {
                if (string.IsNullOrEmpty(context.Set<RTCReceiveRecord>()
                    .Where(p => p.Account == account)
                    .Select(p => p.Account)
                    .SingleOrDefault()))
                {
                    context.Set<RTCReceiveRecord>().Add(record);
                }
                else
                {
                    context.Entry(record).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }

                return false;
            }
        }

        public bool ChangeUserState(string account, bool isOnline)
        {
            using (RTCDbContext context = new RTCDbContext())
            {
                RTCUser user = new RTCUser();
                user.Account = account;
                user.IsOnline = isOnline;

                if (string.IsNullOrEmpty(context.Set<RTCUser>()
                    .Where(p => p.Account == account)
                    .Select(p => p.Account)
                    .SingleOrDefault()))
                {
                    context.Set<RTCUser>().Add(user);
                }
                else
                {
                    context.Entry(user).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public List<RTCUser> GetOnlineUsers()
        {
            using (RTCDbContext context = new RTCDbContext())
            {
                return context.Set<RTCUser>().Where(p => p.IsOnline == true).ToList();
            }
        }
    }
}